package com.example.demo.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.SProduct;
import com.example.demo.common.utils.Response;
import com.example.demo.service.SProductService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags="产品信息服务")
@RestController
@RequestMapping("/product")
public class SProductController {
	@Autowired
	private SProductService spService;
	
	@GetMapping("/all")
	@ApiOperation(value = "查找所有产品信息",notes="分页显示所有产品信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1"),	@ApiImplicitParam(name="pageSize",value = "每页显示个数",defaultValue = "3")	
	})
    public Response findAll(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
		return Response.success(spService.findAllByPage(pageNum, pageSize));
	}
	@GetMapping("/byname")
	@ApiOperation(value = "通过名字查找产品信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1"),	@ApiImplicitParam(name="pageSize",value = "每页显示个数",defaultValue = "3")	
	})
	public Response findByName(String name,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
		return Response.success(spService.findByName(name, pageNum, pageSize));
	}
	@PostMapping("/saveorupdate")
	@ApiOperation(value = "保存或更新产品信息")
	public Response saveOrUpdate(SProduct sp) {
		spService.saveOrupdate(sp);
		return Response.success(sp.getId()==null?"添加产品信息成功":"保存产品信息成功");
	}
	@DeleteMapping("/delete/by/id")
	@ApiOperation(value = "根据id删除")
	public Response deleteById(Integer id) {
		spService.deleteById(id);
		return Response.success("删除id为"+id+"的产品信息成功");
	}
	@DeleteMapping("/batchdelete")
	@ApiOperation(value = "批量删除")
	public Response batchDeleteById(int [] id ) {
		spService.batchDeleteById(id);
		return Response.success("批量删除成功");
	}
}
